package com.nan.controller;

import java.util.List;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.json.FastJson;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.nan.common.RespJson;
import com.nan.controller.base.BaseController;
import com.nan.interceptor.AuthInterceptor;
import com.nan.model.ResInfo;
import com.nan.model.ResType;
import com.nan.model.UserInfo;
import com.nan.model.UserLog;

public class ResourceController extends BaseController {
	
	public void list()
	{
		int page = getParaToInt("page", 1);
		long type = getParaToLong("type", (long) 0);
		String key_word = getPara("search", "").trim();
		int page_size = 20;
		Page<ResInfo> res_list;
		
		String where = " FROM mm_res_info WHERE status=0";
		if (!key_word.equals("") || key_word != null)
		{
			setAttr("key_word", key_word);
			where += " AND title LIKE '%"+key_word+"%'";
		}
		if (type != 0)
		{
			setAttr("type", type);
			where += " AND type="+type;
		}
		res_list = ResInfo.dao.paginate(page, page_size, "SELECT * ", where);
		
		setAttr("is_login", 1);
		setAttr("is_admin", 1);
		UserInfo user = getSessionAttr("mm_login_user");
		if (user == null)
		{
			setAttr("is_login", 0);
			setAttr("is_admin", 0);
		}
		if (user != null && user.getType() == 0)
		{
			setAttr("is_admin", 0);
		}
		
		setAttr("res_list", res_list);
		show("/views/res-list.jsp");
	}
	
	public void detail()
	{
		long res_id = getParaToLong(0, (long) 0);
		
		ResInfo res = ResInfo.dao.findById(res_id);
		setAttr("res", res);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date = sdf.format(new Date(res.getUptime()*1000));
		setAttr("res_time", date);
		
		UserInfo user = UserInfo.dao.findById(res.getUpuser());
		setAttr("user", user);
		
		show("/views/res-detail.jsp");
	}
	
	@Before(AuthInterceptor.class)
	public void edit()
	{
		String sql = "SELECT * FROM mm_res_type WHERE parent=0 AND status=0";
		List<ResType> fst_list = ResType.dao.find(sql);
		setAttr("fst_list", fst_list);
		show("/views/res-upload.jsp");
	}
	
	public void save()
	{
		String id = getPara("id");
		
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		long time = timestamp.getTime() / 1000;
		
		UserInfo user = getSessionAttr("mm_login_user");
		
		ResInfo res_info = new ResInfo();
		
		if (id == null)
		{
			UploadFile res = getFile("file", "/", 1048576000);
			if (res == null)
			{
				showJson(new RespJson(500, "上传失败", res));
				return ;
			}
			res_info.set("filename", res.getFileName());
			res_info.set("filepath", res.getUploadPath());
		}
		else
		{
			res_info = ResInfo.dao.findById(id);
			if (res_info == null)
			{
				showJson(new RespJson(404, "要修改的资源不存在", res_info));
				return ;
			}
		}
		
		res_info.set("type", getPara("type"));
		res_info.set("title", getPara("title"));
		res_info.set("description", getPara("desc"));
		res_info.set("uptime", time);
		res_info.set("upuser", user.getId());
		res_info.set("downtype", getPara("downtype"));
		
		boolean res = false;
		if (id == null)
		{
			res = res_info.save();

			UserLog log = new UserLog();
			log.set("resid", res_info.getId());
			log.set("userid", user.getId());
			log.set("handle", 0);
			log.set("htime", time);
			log.save();
		}
		else
		{
			res = res_info.update();
		}
		
		if (res)
		{
			showJson(new RespJson(200, "", res));
			return ;
		}
		showJson(new RespJson(500, "保存失败", res));
	}
	
	public void delete()
	{
		long id = getParaToLong(0, (long) 0);
		ResInfo res = ResInfo.dao.findById(id);
		res.set("status", 1);
		
		boolean result = res.update();
		if (result)
		{
			showJson(new RespJson(200));
			return ;
		}
		showJson(new RespJson(500, "操作失败"));
		
	}

	
	public void resUpload() {
		Object res = getFile("file", "files", 1048576000);
		if (res == null)
		{
			showJson(new RespJson(500, "上传失败", res));
			return ;
		}
		showJson(new RespJson(200, "ok", res));
	}
	
	public void resDownload() {
		long id = getParaToLong(0, (long) 0);
		if (id != 0)
		{
			Date date = new Date();
			Timestamp timestamp = new Timestamp(date.getTime());
			long time = timestamp.getTime() / 1000;
			
			UserInfo user = getSessionAttr("mm_login_user");
			long user_id = 0;
			if (user != null)
			{
				user_id = user.getId();
			}
			
			
			UserLog log = new UserLog();
			log.set("resid", id);
			log.set("userid", user_id);
			log.set("handle", 1);
			log.set("htime", time);
			log.save();
			
			ResInfo res = ResInfo.dao.findById(id);
			System.out.println(FastJson.getJson().toJson(res));
			renderFile(res.getFilename());
			return ; 
		}
		renderText("File error");
	}
}
